﻿using System;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;

namespace Spacer {
	/// <summary>
	/// Helper static class with general functions such as storage access.
	/// </summary>
	public static class Helper {
		/// <summary>
		/// Retrieves a player's storage device for save information.
		/// </summary>
		/// <param name="player">Player number</param>
		/// <returns>Storage device selected by the user.</returns>
		public static void GetStorageDevice( Callback finished ) {
			if( Globals.GamerServicesEnabled && Guide.IsVisible )
				return;
			
			//StorageDevice device = null;
			IAsyncResult handle = StorageDevice.BeginShowSelector( null, null );
			
			Globals.PushActivity(new PleaseWait(
				handle,
				result => {
					return StorageDevice.EndShowSelector( result );
				},
				finished
			) );

			//handle.AsyncWaitHandle.WaitOne();
			//device = StorageDevice.EndShowSelector( handle );
			//handle.AsyncWaitHandle.Close();

			//return device;
		}

		/// <summary>
		/// Retrieves the storage location within a storage device for data related to this game.
		/// The identifier used for the container is the Assembly name, which should be the namespace (I believe).
		/// </summary>
		/// <param name="device">StorageDevice to retrieve the container from.</param>
		/// <returns>Storage container associated with the selected device.</returns>
		public static StorageContainer GetStorageContainer( StorageDevice device ) {
			if( device == null )
				return null;
			
			Assembly asm = Assembly.GetExecutingAssembly();
			AssemblyName name = asm.GetName();
			
			StorageContainer container;
			IAsyncResult result = device.BeginOpenContainer( name.Name, null, null ); // name.Name SHOULD be "Spacer" but this is a little nicer because it is pulled from the compiled Assembly

			result.AsyncWaitHandle.WaitOne();
			container = device.EndOpenContainer( result );
			result.AsyncWaitHandle.Close();

			return container;
		}

		public static string GetStringInput( string title, string desc, string def ) {
			if( !Globals.GamerServicesEnabled )
				return null;

			string ret = def;
			IAsyncResult result = Guide.BeginShowKeyboardInput( PlayerIndex.One, title, desc, def, null, null );
			
			result.AsyncWaitHandle.WaitOne();
			ret = Guide.EndShowKeyboardInput( result );
			result.AsyncWaitHandle.Close();

			return ret;
		}

		// ----------
		
		public static bool ExistsFile( string filename ) { if( Globals.LocalStorage != null ) return ExistsFile(filename,Globals.LocalStorage); return false; }
		public static bool ExistsFile( string filename, StorageContainer container ) {
			try {
				if( container == null )
					throw new ArgumentNullException( "container" );
				else
					return container.FileExists( filename );
			} catch( Exception e ) {
				Console.WriteLine( e.Message );
			}
			return false;
		}
		
		public static bool ExistsDirectory( string dirname ) { if( Globals.LocalStorage != null ) return ExistsDirectory(dirname,Globals.LocalStorage); return false; }
		public static bool ExistsDirectory( string dirname, StorageContainer container ) {
			try {
				if( container == null )
					throw new ArgumentNullException( "container" );
				else
					return container.DirectoryExists( dirname );
			} catch( Exception e ) {
				Console.WriteLine( e.Message );
			}
			return false;
		}

		public static void DeleteFile( string filename ) { if( Globals.LocalStorage != null ) DeleteFile(filename,Globals.LocalStorage); }
		public static void DeleteFile( string filename, StorageContainer container ) {
			try {
				if( container == null )
					throw new ArgumentNullException( "container" );
				else
					container.DeleteFile( filename );
			} catch( Exception e ) {
				Console.WriteLine( e.Message );
			}
		}

		public static void CreateDir( string dirname ) { if( Globals.LocalStorage != null ) CreateDir(dirname,Globals.LocalStorage); }
		public static void CreateDir( string dirname, StorageContainer container ) {
			try {
				if( container == null )
					throw new ArgumentNullException( "container" );
				else if( container.StorageDevice.IsConnected )
					container.CreateDirectory( dirname );
			} catch( Exception e ) {
				Console.WriteLine( e.Message );
			}
		}
		
		public static void DeleteDir( string dirname ) { if( Globals.LocalStorage != null ) DeleteDir(dirname,Globals.LocalStorage); }
		public static void DeleteDir( string dirname, StorageContainer container ) {
			try {
				if( container == null ) {
					throw new ArgumentNullException( "container" );
				} else {
					string[] files = container.GetFileNames( dirname+'\\' );
					foreach(string file in files) container.DeleteFile( file );
					
					string[] folders = container.GetDirectoryNames( dirname+'\\' );
					foreach(string folder in folders) DeleteDir( folder );

					container.DeleteDirectory( dirname );
				}
			} catch( Exception e ) {
				Console.WriteLine( e.Message );
			}
		}
	}
}
